
const FairyGUI = CS.FairyGUI
const FairyEditor = CS.FairyEditor
const System = CS.System
import UIWind, { WindData } from "./UIWind"
import ToolMgr from '../ToolMgr';
const App = FairyEditor.App;

export default class FindDependencyQuery extends UIWind {

    constructor() {
        super(new WindData("Extend", "FindDependencyQuery"))
    }

    static show() {
        let url = ToolMgr.joinUrl("Extend", "FindDependencyQuery")
        if (UIWind.FindUrl(url) == undefined) {
            UIWind.add(new this)
        }
        super.show(url)
    }

    private result: CS.FairyGUI.GTextField;
    private list1: CS.FairyGUI.GList;
    private list2: CS.FairyGUI.GList;
    private list3: CS.FairyGUI.GList;
    private close: CS.FairyGUI.GButton;


    protected onInit(): void {
        super.onInit();
        this.result = this.contentPane.GetChild("result").asTextField;

        this.list1 = this.contentPane.GetChild("list1").asList;
        this.list2 = this.contentPane.GetChild("list2").asList;
        this.list3 = this.contentPane.GetChild("list3").asList;


        this.contentPane.GetChild("close").onClick.Add(this.onCloseClick.bind(this));


        this.list1.itemRenderer = this.onList1ItemRenderer.bind(this);
        this.list2.itemRenderer = this.onList2ItemRenderer.bind(this);

        this.list1.onClickItem.Add(this.onList1ClickItem.bind(this));
        this.list2.onClickItem.Add(this.onList2ClickItem.bind(this));

        this.list3.itemRenderer = this.onList3ItemRenderer.bind(this);
        this.list3.onClickItem.Add(this.onList3ClickItem.bind(this));

        this.list3.SetVirtual();
    }

    protected onShown(): void {
        super.onShown();
        this.onUpdateShown()
    }

    protected onHide(): void {
        super.onHide();
        this.list1.numItems = 0;
        this.list2.numItems = 0;
        this.list1.data = null;
        this.list2.data = null;
        this.list3.numItems = 0;
        this.list3.data = null;
    }

    private onUpdateShown() {
        let pkgs = []
        for (let index = 0; index < App.project.allPackages.Count; index++) {
            pkgs.push(App.project.allPackages.get_Item(index))
        }
        this.list1.data = pkgs;
        this.list1.numItems = pkgs.length;

        let dic = ToolMgr.createGenericDictionary<string, string>(System.String, System.String);
        dic.Add("count", pkgs.length + "")
        this.result.templateVars = dic;
    }

    private onList1ClickItem(item: CS.FairyGUI.EventContext) {

        let querys = new Array<string>();
        let pkg = item.data.data as CS.FairyEditor.FPackage
        let count = pkg.items.Count

        let list3Array = new Array<CS.FairyEditor.FPackageItem>();

        for (let index = 0; index < count; index++) {
            let item = pkg.items.get_Item(index);
            if (item.type != FairyEditor.FPackageItemType.COMPONENT) {
                continue
            }
            list3Array.push(item)

            let list = ToolMgr.createGenericList<CS.FairyEditor.FPackageItem>(FairyEditor.FPackageItem)
            list.Add(item)
            let data = new FairyEditor.DependencyQuery()
            data.QueryDependencies(list, FairyEditor.DependencyQuery.SeekLevel.SAME_PACKAGE)

            for (let index = 0; index < data.references.Count; index++) {
                let reference = data.references.get_Item(index)
                let pkgID = reference.pkgId
                let yilaipkg = App.project.GetPackage(pkgID)
                if(yilaipkg == null) continue
                if (yilaipkg.GetItem(reference.itemId) && querys.indexOf(yilaipkg.name) == -1 && yilaipkg.name != pkg.name) {
                    querys.push(yilaipkg.name)
                }
            }
        }



        this.list2.data = querys;
        this.list2.numItems = querys.length;

        this.list3.data = list3Array;
        this.list3.numItems = list3Array.length;

    }

    private onList2ClickItem(index: number, item: CS.FairyGUI.GButton) {

    }

    private onList3ClickItem(obj: CS.FairyGUI.EventContext) {
        let querys = new Array<string>();
        let item = obj.data.data as CS.FairyEditor.FPackageItem
        let list = ToolMgr.createGenericList<CS.FairyEditor.FPackageItem>(FairyEditor.FPackageItem)
        list.Add(item)
        let data = new FairyEditor.DependencyQuery()
        data.QueryDependencies(list, FairyEditor.DependencyQuery.SeekLevel.ALL)

        for (let index = 0; index < data.references.Count; index++) {
            let reference = data.references.get_Item(index)
            let pkgID = reference.pkgId
            let yilaipkg = App.project.GetPackage(pkgID)
            if(yilaipkg == null) continue
            if (yilaipkg.GetItem(reference.itemId) && querys.indexOf(yilaipkg.name) == -1) {
                querys.push(yilaipkg.name)
            }
        }

        this.list2.data = querys;
        this.list2.numItems = querys.length;

        App.consoleView.Log(ToolMgr.getUBBUrl(item.GetURL(), item.name));
    }

    private onList1ItemRenderer(index: number, obj: CS.FairyGUI.GButton) {
        let data = this.list1.data[index] as CS.FairyEditor.FPackage;
        obj.text = data.name
        obj.data = data;

        obj.__onDispose = () => {
            obj.text = "";
            obj.data = null;
        }
    }


    private onList2ItemRenderer(index: number, obj: CS.FairyGUI.GButton) {
        let data = this.list2.data[index] as string;
        obj.text = data
        obj.data = data;

        obj.__onDispose = () => {
            obj.text = "";
            obj.data = null;
        }
    }

    private onList3ItemRenderer(index: number, obj: CS.FairyGUI.GButton) {
        let data = this.list3.data[index] as CS.FairyEditor.FPackageItem;
        obj.text = data.name
        obj.data = data;

        obj.__onDispose = () => {
            obj.text = "";
            obj.data = null;
        }
    }

    private onCloseClick() {
        let self = this;
        self.Hide();
        FairyGUI.Timers.inst.Add(0.05, 1, () => {
            UIWind.remove(this)
        })
    }
}

